package com.san.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.base.domain.Tree;
import com.san.common.annotation.Log;
import com.san.common.utils.R;
import com.san.system.domain.MenuDO;
import com.san.system.service.MenuService;

/**
 * @author 
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
	
	String prefix = "system/menu";
	@Autowired
	MenuService menuService;

	@RequiresPermissions("sys:menu:view")
	@GetMapping()
	String menu(Model model) {
		return prefix+"/view";
	}

	@RequiresPermissions("sys:menu:view")
	@RequestMapping("/list")
	@ResponseBody
	List<MenuDO> list(@RequestParam Map<String, Object> params) {
		List<MenuDO> menus = menuService.list(params);
		return menus;
	}

	@Log("添加菜单")
	@RequiresPermissions("sys:menu:add")
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			try {
				MenuDO menuDO = (MenuDO) menuService.getById(pId);
				model.addAttribute("pName", menuDO.getName());
			} catch (Exception e) {
				logger.error("获取菜单名称失败",e);
			}
			
		}
		return prefix + "/add";
	}

	@Log("编辑菜单")
	@RequiresPermissions("sys:menu:edit")
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		try {
			MenuDO mdo = (MenuDO) menuService.getById(id);
			Long pId = mdo.getParentId();
			model.addAttribute("pId", pId);
			if (pId == 0) {
				model.addAttribute("pName", "根目录");
			} else {
				MenuDO menuDO = (MenuDO) menuService.getById(pId);
				model.addAttribute("pName", menuDO.getName());
			}
			model.addAttribute("menu", mdo);
		} catch (Exception e) {
			logger.error("获取菜单名称失败",e);
		}
		return prefix+"/edit";
	}

	@Log("保存菜单")
	@RequiresPermissions("sys:menu:add")
	@PostMapping("/save")
	@ResponseBody
	R save(MenuDO menu) {
		try {
			if (menuService.save(menu) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("新增菜单失败",e);
		}
		return R.error(1, "保存失败");
	}

	@Log("更新菜单")
	@RequiresPermissions("sys:menu:edit")
	@PostMapping("/doEdit")
	@ResponseBody
	R doEdit(MenuDO menu) {
		try {
			if (menuService.doEdit(menu) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("更新菜单失败",e);
		}
		return R.error(1, "更新失败");
	}

	@Log("删除菜单")
	@RequiresPermissions("sys:menu:remove")
	@PostMapping("/remove")
	@ResponseBody
	R remove(Long id) {
		try {
			if (menuService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("删除菜单失败",e);
		} 
			
		return R.error(1, "删除失败");
		
	}

	@GetMapping("/tree")
	@ResponseBody
	Tree<MenuDO> tree() {
		Tree<MenuDO>  tree = menuService.getTree();
		return tree;
	}

	@GetMapping("/tree/{roleId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("roleId") Long roleId) {
		Tree<MenuDO> tree = menuService.getTree(roleId);
		return tree;
	}
}
